package de.grosjohann.android.feuerwehr.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import de.grosjohann.android.feuerwehr.FMSActivity;
import de.grosjohann.android.feuerwehr.IUpdateUIListener;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.util.FileUtil;
import de.grosjohann.android.feuerwehr.util.PrefsUtil;
import de.grosjohann.android.feuerwehr.webservice.FMSWsFacade;

public class FMSNotificationService
  extends IntentService {
  public static final String FMS_STATUS_AUTHORIZATION = "feuerwehr.FMSStatusAuthorization";
  public static final String FMS_STATUS_KENNUNG = "feuerwehr.FMSStatusKennung";
  public static final String FMS_STATUS_DATA = "feuerwehr.FMSStatusData";
  public static final String FMS_STATUS_ERROR = "feuerwehr.FMSStatusError";

  public static final String HTTP_METHOD = "feuerwehr.HTTP_METHOD";
  public static final String HTTP_METHOD_POST = "POST";
  public static final String HTTP_METHOD_GET = "GET";

  public static final int FMS_SERVICE_NOTIFICATION_ID = 112;

  private static final String NAME = "FMSNotificationService";

  public static IUpdateUIListener updateUIListener;

  private Notification notification = null;

  public FMSNotificationService() {
    super(NAME);
  }

  @Override
  public void onCreate() {
    super.onCreate();

    Log.d(NAME, "create");

    // Notification aufbauen
    notification = new Notification();
    notification.tickerText = getString(R.string.lblFMSNotificationTickerText);
    notification.icon = R.drawable.notificon;
  }

  @Override
  protected void onHandleIntent(Intent newIntent) {

    Log.d(NAME, "handle");

    Context context = getApplicationContext();

    Bundle bundle = newIntent.getExtras();
    String methode =
        (bundle == null) ? "" : (bundle.getString(FMSNotificationService.HTTP_METHOD) == null) ? "" : bundle
            .getString(FMSNotificationService.HTTP_METHOD);

    if (bundle == null || methode.length() == 0 || FMSNotificationService.HTTP_METHOD_GET.equals(methode)) {
      getStatus(context);
    } else if (FMSNotificationService.HTTP_METHOD_POST.equals(bundle.get(FMSNotificationService.HTTP_METHOD))) {
      setStatus(context, bundle);
    }
  }

  private void getStatus(Context context) {
    try {
      String json = FMSWsFacade.getFMSStatus(context);
      String lastReceive = FileUtil.readFile(context, FMS_STATUS_DATA);

      if (json != null && !json.equals(lastReceive)) {

        FileUtil.writeFile(context, FMS_STATUS_DATA, json);
        Log.i(NAME, "new Data: " + json);

        if (PrefsUtil.getBoolean(context, R.string.prefKeyNotification, false)) {
          Intent notificationIntent = new Intent(context, FMSActivity.class);

          PendingIntent contentIntent =
              PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

          // expanded message title
          CharSequence contentTitle = getString(R.string.appName);

          // expanded message text
          CharSequence contentText = getString(R.string.lblFMSNotificationTickerText);

          notification.when = System.currentTimeMillis();

          notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

          NotificationManager mNotificationManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          mNotificationManager.notify(FMS_SERVICE_NOTIFICATION_ID, notification);
        }
      }

    } catch (Exception ex) {
      Log.e(NAME, ex.getMessage(), ex);
    }
  }

  private void setStatus(Context context, Bundle bundle) {
    try {
      String json =
          FMSWsFacade.setFMSStatus(context, bundle.getString(FMSNotificationService.FMS_STATUS_AUTHORIZATION),
              bundle.getString(FMSNotificationService.FMS_STATUS_KENNUNG),
              bundle.getString(FMSNotificationService.FMS_STATUS_DATA));

      FileUtil.writeFile(context, FMS_STATUS_DATA, json);

      if (updateUIListener != null) {
        updateUIListener.showInfo("");
      }
    } catch (Exception ex) {
      Log.e(NAME, ex.getMessage(), ex);
      if (updateUIListener != null) {
        updateUIListener.showError("");
      }

    }
  }
}
